package com.luomo.java.se;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiuMei
 * @date 2021-04-19.
 */
public class TwoSumDemo {

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int sub = target-nums[i];
            Integer key = map.get(sub);
            if (key == null) {
                map.put(nums[i], i);
            } else {
                return new int[]{map.get(sub), i};
            }
        }
        return null;
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        int[] ints = twoSum(nums, target);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

}
